var UnicodeORGImage = GownFullModule.extend({
	ime: null,
	constructor: function()
	{
		var y = this;

		this._unicodeimage_functional = function(x) {
			if(x instanceof Function) {
				var dxchg = x();

				if(dxchg && !dxchg.htmlp) {
					// Create new data exchange to make the word become HTML...
					var n = {};
					n.target = dxchg.target;
					n.value = y.unicode_to_image(dxchg.value);
					n.htmlp = true;
					return GFFunctionalInterpreter.CreateJSFunctional(n);
				}
				return x;
			}
			else return x;
		}
		this._unicodetrace_functional = function(x) {
			if(x instanceof Function) {
				var dxchg = x();

				if(dxchg && !dxchg.htmlp) {
					Debug.present && Debug.trace(1,"UnicodeORGImage (trace): %s",y.unicode_to_image(dxchg.value));
				}
				return x;
			}
			else return x;
		}
	},
	module_name: function()
	{
		return 'UnicodeORGImage';
	},
	init_module: function(ime,modman)
	{
		var b;

		this.ime = ime;
		
		b = GFFunctionalInterpreter.RegisterFunctional('unicodeimage',this._unicodeimage_functional);
		if(!b) {
			Debug.present && Debug.trace(0,'UnicodeORGImage: Error, functional :unicodeimage is already registered.');
			return false;
		}
		b = GFFunctionalInterpreter.RegisterFunctional('unicodetrace',this._unicodetrace_functional);
		if(!b) {
			Debug.present && Debug.trace(0,'UnicodeORGImage: Error, functional :unicodetrace is already registered.');
			return false;
		}

		return true;
	},
	cleanup_module: function()
	{
		GFFunctionalInterpreter.UnregisterFunctional('unicodeimage');
		GFFunctionalInterpreter.UnregisterFunctional('unicodetrace');
		return true;
	},
	unicode_to_image: function(str,linkp)
	{
		var i,len,utf16;
		var html;

		for(i=0;i<str.length;) {
			len = lib.utf16charlen(str.substr(i));
			utf16 = lib.chartoutf16(str.substr(i));
			if(linkp) html = lib.sprintf('<a href="http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=%x">',utf16);
			else html = '';

			html += lib.sprintf('<img alt="&#%d;" src="http://www.unicode.org/cgi-bin/refglyph?24-%x" style="border-style:none;" />',utf16,utf16);
			
			if(linkp) html += '</a>';

			i += len;
		}
		return html;
	}
});
